﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Admin_NewsletterChange : BasePage
{
    private int _ID = 0;
    private bool _IsEditMode = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindDropDownLists();
            BindNewsLetterInfo();
            ShowSuccessMessage();
        }
    }
    /// <summary>
    /// Bindis the Page Initialization Variables
    /// </summary>
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        if (_ID > 0)
        {
            _IsEditMode = true;
            h4Heading.InnerHtml = "Edit Newsletter";
        }
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }    
    /// <summary>
    /// Binds Dropdownlists for the initial request.
    /// </summary>
    protected void BindDropDownLists()
    {
        //BindDropdownList.Users(ddlModifiedBy);

    }
    protected void ShowSuccessMessage()
    {
        if(String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.SUCCESS_MESSAGE), "True", false) == 0)
        {
            WebUtil.ShowMessageBox(divMessage, "Newsletter Saved Successfully.", false);
        }
    }
    /// <summary>
    /// Binds NewsLetter Info Requested through Query Strings
    /// </summary>
    protected void BindNewsLetterInfo()
    {
        if (_IsEditMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            NewsLetter entity = context.NewsLetters.FirstOrDefault(P => P.ID == _ID);//dao.GetByID(_ID);
            if (entity == null)
                ShowNotFoundMessage();
            else
            {
                txtTitle.Text = entity.Title;
                txtHtml.Text = entity.Html;
                chkIsActive.Checked = entity.IsActive;
            }
        }
    }
    /// <summary>
    /// Shows a Message in the UI and Hides the Data Editing Controls
    /// </summary>
    protected void ShowNotFoundMessage()
    {
        pnlFormContainer.Visible = false;
        WebUtil.ShowMessageBox(divMessage, "Requested Newsletter was not found.", true);
    }
    protected void SaveNewsLetter()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        NewsLetter entity = null;

        if (_IsEditMode)
            entity = context.NewsLetters.FirstOrDefault(P => P.ID == _ID); //dao.GetByID(_ID);        
        else
        {
            entity = new NewsLetter();
            entity.Created = DateTime.Now;
            context.AddToNewsLetters(entity);
        }

        entity.Title = txtTitle.Text;
        entity.Html = txtHtml.Text;
        entity.IsActive = chkIsActive.Checked;
        entity.Modified = DateTime.Now;
        entity.ModifiedBy = SessionCache.CurrentUser.ID;
        context.SaveChanges();       

        String url = String.Format("{0}?{1}=True", Request.Url.AbsolutePath, AppConstants.QueryString.SUCCESS_MESSAGE);
        Response.Redirect(url);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveNewsLetter();
            //Response.Redirect(AppConstants.Pages.NEWSLETTER_LIST);
            return;
        }
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        //Response.Redirect(AppConstants.Pages.NEWSLETTER_LIST);
        return;
    }
}